package com.qyhl.module_activities.act.player.list;


import com.google.gson.Gson;
import com.qyhl.module_activities.R;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.constant.ErrorCode;
import com.qyhl.webtv.commonlib.entity.act.PlayerVOBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by helin on 2018/1/11.
 */
public class PlayerListModel implements PlayerListContract.PlayerListModel {

    private final PlayerListContract.PlayerListPresenter mPersenter;

    public PlayerListModel(PlayerListContract.PlayerListPresenter presenter) {
        mPersenter = presenter;
    }

    @Override
    public void getPlayerLiset(String actId, final String page, String type) {

        EasyHttp.get(ActUrl.ACTIVITY_PLAYER_LIST)
                .params("actId", actId)
                .params("page", page)
                .params("type", type)
                .execute(new SimpleCallBack<List<PlayerVOBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPersenter.onError(1, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                    }

                    @Override
                    public void onSuccess(List<PlayerVOBean> playerVOBeans) {
                        mPersenter.onSuccess(playerVOBeans, page);
                    }
                });

    }

    @Override
    public void getPlayerListByTime(String actId, final String playerId, String type) {

        EasyHttp.get(ActUrl.ACTIVITY_PLAYER_LIST_BY_TIME)
                .params("actId", actId)
                .params("playerId", playerId)
                .params("type", type)
                .execute(new SimpleCallBack<List<PlayerVOBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPersenter.onError(1, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                    }

                    @Override
                    public void onSuccess(List<PlayerVOBean> playerVOBeans) {
                        mPersenter.onSuccess(playerVOBeans, playerId);
                    }
                });

        
    }

    @Override
    public void vote(int actId, String userName, Integer playerId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = ActUrl.ACTIVITY_VOTE_ENCRYPTION + "?timestamp=" + time;
        
        
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "actId=" + actId + "&userName=" + userName +
                        "&playerId=" + playerId + "&number=1"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPersenter.onError(2, "无网络连接，请检查您的网络...");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        switch (apiResult.getCode()) {
                            case ErrorCode.VOTE_STATUS_1:
                                mPersenter.onError(2, "您已用完今日票数");
                                break;
                            case ErrorCode.VOTE_STATUS_2:
                                mPersenter.onError(2, "本次活动您的票数已用完");
                                break;
                            case ErrorCode.VOTE_STATUS_3:
                                mPersenter.onError(2, apiResult.getMessage());
                                break;
                            case ErrorCode.VOTE_STATUS_4:
                            case ErrorCode.VOTE_STATUS_5:
                                mPersenter.onError(2, "对同一选手只能投一票");
                                break;
                            case ErrorCode.SUCCESS:
                                mPersenter.onVoteSuccess();
                                break;
                            default:
                                mPersenter.onError(2, apiResult.getMessage());
                                break;
                        }
                    }
                });
        
    }

    @Override
    public void searchData(String actId, String text) {
        
        EasyHttp.get(ActUrl.ACTIVITY_PLAYER_SEARCH)
                .params("actId", actId)
                .params("text", text)
                .execute(new SimpleCallBack<List<PlayerVOBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPersenter.onError(3, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                    }

                    @Override
                    public void onSuccess(List<PlayerVOBean> playerVOBeans) {
                        mPersenter.onSearchSuccess(playerVOBeans);
                    }
                });
        

    }
}
